package com.xx.track_log.log;

import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;

/**
 * @author fanjj
 * @date 2017/10/19
 */
public class TrackLogger {
    private ThreadLocal<LoggingEventProcessor> loggingEventProcessor = new ThreadLocal<>();
    private static TrackLogger instance = new TrackLogger();
    private String logFolder;//TrackAppender初始化的时候将logFolder赋值
    private String logLevel;//TrackAppender初始化的时候将logLevel赋值

    public static TrackLogger getInstance() {
        return instance;
    }

    public void process(LoggingEvent event, Layout layout) {
        LoggingEventProcessor processor = loggingEventProcessor.get();
        if (null != processor) {
            processor.process(event, layout);
        }
    }

    public void cleanUp() {
        LoggingEventProcessor processor = loggingEventProcessor.get();
        processor.cleanup(); // finally close writer to end write trace log
        loggingEventProcessor.remove();
    }

    public void initialize(String actionName) {
        loggingEventProcessor.set(new LoggingEventProcessor(logFolder, actionName, logLevel));
    }

    public void setLogFolder(final String logFolder) {
        this.logFolder = logFolder;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }
}
